package ldh.fx.adminfx.commpont.grid;

import java.util.List;

public class PageResult<T> {
    private long pageNo;
    private int pageSize;
    private long pageTotal;
    private long total;
    private List<T> beans;

    public PageResult() {
    }

    public PageResult(long total, List<T> beans) {
        this(1L, 10, total, beans);
    }

    public PageResult(Pageable pagination, long total, List<T> beans) {
        int length = pagination.getLength();
        if (length < 0) {
            length = 10;
        }

        this.pageNo = pagination.getPageNo();
        if (this.pageNo < 1L) {
            this.pageNo = 1L;
        }

        this.pageSize = length;
        this.total = total;
        this.pageTotal = total / (long)length;
        if (this.pageTotal * (long)length < total) {
            ++this.pageTotal;
        }

        this.beans = beans;
    }

    public PageResult(long pageNo, int pageSize, long total, List<T> beans) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
        this.beans = beans;
        this.handlePage();
    }

    public long getPageNo() {
        return this.pageNo;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return this.total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getBeans() {
        return this.beans;
    }

    public void setBeans(List<T> beans) {
        this.beans = beans;
    }

    public void handlePage() {
        if (this.pageSize < 0) {
            this.pageSize = 10;
        }

        this.pageTotal = this.total / (long)this.pageSize;
        if (this.pageTotal * (long)this.pageSize < this.total) {
            ++this.pageTotal;
        }

    }

    public void setPageNo(long pageNo) {
        this.pageNo = pageNo;
    }

    public boolean isFirst() {
        return this.pageNo == 0L;
    }

    public boolean isEnd() {
        return this.pageNo == this.pageTotal - 1L;
    }

    public boolean hasNext() {
        return this.pageNo < this.pageTotal - 1L;
    }

    public boolean hasPrevious() {
        return this.pageNo > 0L;
    }

    public long getPageTotal() {
        return this.pageTotal;
    }

    public void next() {
        if (this.hasPrevious()) {
            ++this.pageNo;
        }

        throw new IndexOutOfBoundsException("没有下一页");
    }

    public void previous() {
        if (this.hasPrevious()) {
            --this.pageNo;
        }

        throw new IndexOutOfBoundsException("没有上一页");
    }
}
